package CI.aufgabe2.cip.mynodes;

import java.nio.channels.Selector;
import java.util.List;

import CI.aufgabe2.cip.SymbolTable;
import CI.aufgabe2.cip.base.AbstractDescr;
import CI.aufgabe2.cip.descriptor.ArrayDescr;
import CI.aufgabe2.cip.descriptor.RecordDescr;
import CI.aufgabe2.cip.descriptor.SimpleTypeDescr;

public class MyAssignmentNode extends MyAbstractNode {

	private static final long serialVersionUID = 1L;
	
	private final MyAbstractNode selector;
	private final MyAbstractNode expression;
	
	public MyAssignmentNode(MyAbstractNode selector, MyAbstractNode expression) {
		this.selector=selector;
		this.expression=expression;
	}
	

	@Override
	public void print() {
		trace("AssignmentNode ");
//		if(ident != null)
//		ident.print();
		
		if(selector != null)
		selector.print();
		
		if(expression != null)
		expression.print();
		
		unindent();

	}
	
	
	public AbstractDescr compile(SymbolTable sym) {
		
		AbstractDescr rDescr = expression.compile(sym);
		List<MyIdNode> varList = sym.getVarFormalParameters();
		if(rDescr instanceof SimpleTypeDescr && (expression instanceof MyIdNode || expression instanceof MyArraySelectorNode ||expression instanceof MyRecordSelectorNode)){
			write("CONT, 1");
			selector.compile(sym);
			if(varList.contains(selector))
				write("CONT, 1");
		}else if(rDescr instanceof ArrayDescr || rDescr instanceof RecordDescr){
			AbstractDescr lDescr = selector.compile(sym);
			if(rDescr instanceof ArrayDescr && lDescr instanceof ArrayDescr)
				sym.setAdr(((MyIdNode)selector).strval, sym.getAdr(((MyIdNode)expression).strval));
		}else{
			selector.compile(sym);
			if(varList.contains(selector))
				write("CONT, 1");
		}
		
		write("ASSIGN, 1");
		
		return null;
	}

}
